﻿using System;
using System.Drawing;

namespace FontTool
{
    public partial class FontToolMain
    {
        #region variables
        //all the lines exist only in editor
        public nLine _base;
        public nLine _multiBase;
        public nLine _Leading;
        public nLine _xHeight;
        public nLine _capHeight;
        public nLine _ascenderHeight;
        public nLine _descenderHeight;

        //for these two I will need extra lines since they have begining and end coord when drawed;
        //in practical ussage its one since its just used as part of drawing spacing;
        public nLine _SpaceBegin;
        public nLine _SpaceEnd;
        public nLine _KerningBegin;
        public nLine _KerningEnd;

        //extracted location of the first space occurance in preview window, so I could draw spaceBegin;
        public float _SpaceBeginLocation = 0;
        public float _KerningBeginLocation = 0;

        Color _DefaultBackColor = Color.Black;
        Color _SelectedBackColor = Color.Green;
        Color _SpecialBackColor = Color.Red;

        float moveLineFactor = 0.1f;
        #endregion

        #region Lines init methods
        /*
         * creating a default line going through the middle of panelPreview;
         * creating base,leading,xheight,capheight,ascenderheight and descenderheight lines;
         */
        public void LinesInitHorizontal()
        {
            if (previewDefault)
            {
                _base.init(5, panelPreview.Height / 2, panelPreview.Width - 5, panelPreview.Height / 2);
            }

            float multiLineY = _base.Location.Y + newFont._ascenderHeight + newFont._descenderHeight + newFont._Leading;
            _multiBase.init(_base.Location.X, multiLineY, _base.Width, multiLineY);

            //Initialize lines from Font data;
            _Leading.init(_base.Location.X, _base.Location.Y + newFont._Leading + newFont._descenderHeight,
                _base.Width, _base.Height + newFont._Leading + newFont._descenderHeight);           

            _xHeight.init(_base.Location.X, _base.Location.Y - newFont._xHeight,
                _base.Width, _base.Height - newFont._xHeight);          

            _capHeight.init(_base.Location.X, _base.Location.Y - newFont._capHeight,
                _base.Width, _base.Height - newFont._capHeight);           

            _ascenderHeight.init(_base.Location.X, _base.Location.Y - newFont._ascenderHeight,
                _base.Width, _base.Height - newFont._ascenderHeight);          

            _descenderHeight.init(_base.Location.X, _base.Location.Y + newFont._descenderHeight,
                _base.Width, _base.Height + newFont._descenderHeight);           
        }

        public void initEmptyLines()
        {
            _base = new nLine();
            _base.Name = "Base";
            _base.BackColor = _SpecialBackColor;

            _multiBase = new nLine(); 
            _multiBase.Name = "MultiBase";
            _multiBase.BackColor = _SpecialBackColor;

            _Leading = new nLine();
            _Leading.Name = "Leading";

            _xHeight = new nLine();
            _xHeight.Name = "xHeight";

            _capHeight = new nLine(); 
            _capHeight.Name = "capHeight";

            _ascenderHeight = new nLine();
            _ascenderHeight.Name = "ascenderHeight";

            _descenderHeight = new nLine();
            _descenderHeight.Name = "descenderHeight";

            _SpaceBegin = new nLine();
            _SpaceBegin.Name = "spaceBegin";

            _SpaceEnd = new nLine();
            _SpaceEnd.Name = "spaceEnd";
            _SpaceEnd.BackColor = _SpecialBackColor;

            _KerningBegin = new nLine();
            _KerningBegin.Name = "kerningBegin";

            _KerningEnd = new nLine();
            _KerningEnd.Name = "kerningEnd";
            _KerningEnd.BackColor = _SpecialBackColor;
        }

        public void initVerticalLines()
        {
            _SpaceBegin.init(_SpaceBeginLocation, 10, _SpaceBeginLocation, panelPreview.Height);
            _SpaceEnd.init(_SpaceBegin.Location.X + newFont._Space, _SpaceBegin.Location.Y, _SpaceBegin.Location.X + newFont._Space, _SpaceBegin.Height);
            _KerningBegin.init(_KerningBeginLocation, 10, _KerningBeginLocation, panelPreview.Height);
            _KerningEnd.init(_KerningBegin.Location.X + newFont._Kerning, _KerningBegin.Location.Y, _KerningBegin.Location.X + newFont._Kerning, _KerningBegin.Height);
        }
        #endregion

        #region Lines update
        /*
         * makes all likes clear
         * draw those which has properties set in newFont as true;
         */
        public void UpdateLines(Graphics graphics)
        {
            if (newFont.LineBase)
                drawLine(_base, graphics); 
            if (newFont.LineMultiBase)
                drawLine(_multiBase, graphics);
            if (newFont.LineLeading)
                drawLine(_Leading, graphics);
            if (newFont.LineXHeight)
                drawLine(_xHeight, graphics);
            if (newFont.LineCapHeight)
                drawLine(_capHeight, graphics);
            if (newFont.LineAscenderHeight)
                drawLine(_ascenderHeight, graphics);
            if (newFont.LineDescenderHeight)
                drawLine(_descenderHeight, graphics);
            if (newFont.LineSpace)
            {
                drawLine(_SpaceBegin, graphics);
                drawLine(_SpaceEnd, graphics);
            }
            if (newFont.LineKerning)
            {
                drawLine(_KerningBegin, graphics);
                drawLine(_KerningEnd, graphics);
            }
        }
        #endregion

        #region lines Draw
        public void drawLine(nLine line, Graphics graphics)
        {
            Pen pen = new Pen(line.BackColor, 1);
            gfx.DrawLine(pen, line.Location.X, line.Location.Y, line.Width, line.Height);

            Point position = new Point((int)line.Location.X, (int)(line.Location.Y - 10));
            if (line.Name == "Leading" || line.Name == "xHeight" || line.Name == "Base" || line.Name == "ascenderHeight" || line.Name == "MultiBase")
                position.X = (int)line.Width - (int)gfx.MeasureString(line.Name, fFont).Width;
            if (line.Name == "spaceEnd" || line.Name == "kerningEnd")
                position.Y = (int)line.Location.Y + 10;
            graphics.DrawString(line.Name, fFont, new SolidBrush(line.BackColor), position);
        }
        #endregion

        #region Lines helper
        public void checkLinesIntersection(Point mouseLocation)
        {
            //lineIntersects(_base, mouseLocation);
            lineIntersects(_Leading, mouseLocation);
            lineIntersects(_xHeight, mouseLocation);
            lineIntersects(_capHeight, mouseLocation);
            lineIntersects(_ascenderHeight, mouseLocation);
            lineIntersects(_descenderHeight, mouseLocation);

            //lineIntersects(_SpaceBegin, mouseLocation);
            //lineIntersects(_KerningBegin, mouseLocation);
        }

        private void lineIntersects(nLine line, Point mouseLocation)
        {
            if (mouseLocation.X >= line.Location.X && mouseLocation.X <= line.Width)
                if(mouseLocation.Y >= line.Location.Y || mouseLocation.Y+2 >=line.Location.Y)
                    if(mouseLocation.Y <= line.Height || mouseLocation.Y-2 <= line.Height)
            {
                
                setAllLinesUnmovable();
                line.movable = true;
                line.BackColor = _SelectedBackColor;

                switch (line.Name)
                {
                    case "Leading":
                        newFont.selectedLine = nFont.Lines._Leading;
                        break;
                    case "xHeight":
                        newFont.selectedLine = nFont.Lines._xHeight;
                        break;
                    case "capHeight":
                        newFont.selectedLine = nFont.Lines._capHeight;
                        break;
                    case "ascenderHeight":
                        newFont.selectedLine = nFont.Lines._ascenderHeight;
                        break;
                    case "descenderHeight":
                        newFont.selectedLine = nFont.Lines._descenderHeight;
                        break;
                    default:
                        break;
                }
                UpdatePreviewWindow();
            }
        }

        private void setAllLinesUnmovable()
        {
            _Leading.movable = false;
            _xHeight.movable = false;
            _capHeight.movable = false;
            _ascenderHeight.movable = false;
            _descenderHeight.movable = false;

            _Leading.BackColor = _DefaultBackColor;
            _xHeight.BackColor = _DefaultBackColor;
            _capHeight.BackColor = _DefaultBackColor;
            _ascenderHeight.BackColor = _DefaultBackColor;
            _descenderHeight.BackColor = _DefaultBackColor;

            //newFont.selectedLine = nFont.Lines.None;
        }

        public void moveLine(bool up)
        {
            if (_Leading.movable)
            {
                if (up)
                    newFont.Leading += moveLineFactor;
                if (!up)
                    newFont.Leading -= moveLineFactor;
            }
            if (_xHeight.movable)
            {
                if (up)
                    newFont.xHeight += moveLineFactor;
                if (!up)
                    newFont.xHeight -= moveLineFactor;
            }
            if (_capHeight.movable)
            {
                if (up)
                    newFont.capHeight += moveLineFactor;
                if (!up)
                    newFont.capHeight -= moveLineFactor;
            }
            if (_ascenderHeight.movable)
            {
                if (up)
                    newFont.ascenderHeight += moveLineFactor;
                if (!up)
                    newFont.ascenderHeight -= moveLineFactor;
            }
            if (_descenderHeight.movable)
            {
                if (up)
                    newFont.descenderHeight += moveLineFactor;
                if (!up)
                    newFont.descenderHeight -= moveLineFactor;
            }

            UpdatePreviewWindow();
        }
        #endregion
    }
}
